package hlhj.fhp.tvlib.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.tenma.ventures.config.TMServerConfig
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.TvListBean
import kotlinx.android.synthetic.main.activity_home_aty.*
import java.text.DecimalFormat
import java.util.ArrayList

class HomeAty : BaseAty() {
    private lateinit var adp:BaseQuickAdapter<TvListBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<TvListBean.DataBean>()
    private lateinit var loadingDialog:LoadingDialog
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Contents.BaseUrl= TMServerConfig.BASE_URL

        Contents.IMG_URL= TMServerConfig.BASE_URL+"/"
        Log.e("fhp","天马的域名${TMServerConfig.BASE_URL}")
        loadingDialog= LoadingDialog(this)
        initRcy()
        setSp()
        initListener()
    }

    override fun getContentId(): Int {
        return R.layout.activity_home_aty
    }
    private fun setSp() {
        spView.header=DefaultHeader(this)
        spView.setListener(object :SpringView.OnFreshListener{
            override fun onLoadmore() {
            }

            override fun onRefresh() {
                datas.clear()
                getData()
            }
        })
    }

    private fun initRcy() {
        adp=object :BaseQuickAdapter<TvListBean.DataBean,BaseViewHolder>(R.layout.home_item,datas){
            override fun convert(helper: BaseViewHolder?, item: TvListBean.DataBean?) {
                helper?.setText(R.id.itemTittle,item?.channel_name)

            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        getData()
    }

    private fun getData() {
        OkGo.get<String>(Contents.BaseUrl+Contents.TV_LIST)
                .execute(object :StringCallback(){
                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                        loadingDialog.show()
                    }
                    override fun onSuccess(response: Response<String>?) {
                        spView.onFinishFreshAndLoad()
                        loadingDialog.dismiss()
                        val listBean = Gson().fromJson(response!!.body(), TvListBean::class.java)
                        if (listBean.code==1){
                            datas.addAll(listBean.data)
                            adp.notifyDataSetChanged()
                        }
                    }
                })
    }

    private fun initListener() {
        btTv.setOnClickListener {
            val intent = Intent(this, TvAty::class.java)
            intent.putExtra("tid",datas[0].id)
            intent.putExtra("tittle",datas[0].channel_name)
            intent.putExtra("source",datas[0].tv_source)
            startActivity(intent)
        }
        btLive.setOnClickListener {
            val intent = Intent(this, LiveListAty::class.java)
            startActivity(intent)
        }
        btBroad.setOnClickListener {
            val intent = Intent(this, BroadCastAty::class.java)
            startActivity(intent)
        }
        adp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this@HomeAty, TvAty::class.java)
            intent.putExtra("tid",datas[position].id)
            intent.putExtra("tittle",datas[position].channel_name)
            intent.putExtra("source",datas[position].tv_source)
            startActivity(intent)
        }
    }

}

